﻿using NPinyin;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Extensions.Common;
using Wq.Extensions.Repository;
using Wq.ManagerService.Entities;
using Wq.ManagerService.Models.Region;
using Wq.Model;

namespace Wq.ManagerService
{
    /// <summary>
    /// 已开通的城市
    /// </summary>
    public class OpenCityService : SqlSugarBaseRepository<OpenCityInfo>, IAddSingleton
    {
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="model"></param>
        /// <exception cref="MyException"></exception>
        private void Verify(OpenCityInfo model)
        {
            if (model.RegionId == 0)
                throw new MyException("请选择城市");
            if (model.Business.Count == 0)
                throw new MyException("请添加商圈");
            model.Business.ForEach(item =>
            {
                if (string.IsNullOrEmpty(item.Name))
                    throw new MyException("商圈名称不能为空");
            });
           
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="info"></param>
        public void Create(OpenCityInfo info)
        {
            Verify(info);
            if(base.Count(p => p.RegionId == info.RegionId) > 0)
                throw new MyException("该城市已添加");
            Context.InsertNav(info)
            .Include(p => p.Business)
            .ExecuteCommand();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="info"></param>
        public void Save(OpenCityInfo info)
        {
            Verify(info);
            Context.UpdateNav(info)
            .Include(p => p.Business)
            .ExecuteCommand();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="regionId"></param>
        public void Delete(int regionId)
        {
            Context.Ado.UseTran(() =>
            {
                base.DeleteById(regionId);
                Context.Deleteable<BusinessCircleInfo>().Where(p => p.CityRegionId == regionId).ExecuteCommand();
            });
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public OpenCityInfo Details(int regionId) =>
             Context.Queryable<OpenCityInfo>().Where(p => p.RegionId == regionId)
                .Includes(p => p.Business).First();

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateSequence(long regionId, long sequence) => Context.Updateable<OpenCityInfo>().SetColumns(p => p.Sequence == sequence).Where(p => p.RegionId == regionId).ExecuteCommand();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="pageModel"></param>
        /// <returns></returns>
        public List<OpenCityView> List(PageModel<OpenCityQuery> pageModel)
        {
            int count = 0;
            var list = Context.Queryable<OpenCityInfo>().LeftJoin<RegionInfo>((o, r) => o.RegionId == r.Id)
                  .WhereIF(!string.IsNullOrWhiteSpace(pageModel.Entity.Name), (o, r) => r.Name.Contains(pageModel.Entity.Name))
                  .Select<OpenCityView>()
                  .ToPageList(pageModel.Page.Index, pageModel.Page.Size, ref count);

            var ids = list.Select(p => p.RegionId).ToList();
           var businesss = Context.Queryable<BusinessCircleInfo>().Where(p => ids.Contains(p.CityRegionId)).ToList();
            list.ForEach(item =>
            {
                item.Business = businesss.Where(p => p.CityRegionId == item.RegionId).ToDictionary(p => p.Id, p => p.Name);
            });
            return list;
        }

        /// <summary>
        /// 获取全部开放城市
        /// </summary>
        /// <returns></returns>
        public List<OpenCityView> List() => Context.Queryable<OpenCityInfo>().LeftJoin<RegionInfo>((o, r) => o.RegionId == r.Id)
                  .OrderByDescending((o, r) => o.Sequence)
                  .Select<OpenCityView>()
                  .ToList();

        /// <summary>
        /// 修改是否热门
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="hot"></param>
        public void UpdateHot(int regionId,bool hot) => Context.Updateable<OpenCityInfo>().SetColumns(p => p.Hot == hot).Where(p => p.RegionId == regionId).ExecuteCommand();

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public OpenCityView Details(string name) => Context.Queryable<OpenCityInfo>().LeftJoin<RegionInfo>((o, r) => o.RegionId == r.Id)
                  .Where((o, r) => r.Name == name)
                  .Select<OpenCityView>()
                  .First();
    }
}
